package com.fifth.apartment.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.apartment.constant.MessageConstant;
import com.fifth.apartment.entity.Administrators;
import com.fifth.apartment.result.Result;
import com.fifth.apartment.service.AdministratorsService;
import com.fifth.apartment.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * TODO
 *
 * @author Guzhenghui
 * @since 2024/7/2
 */
@RestController
@RequestMapping("/admin/Admini")
@Api(tags = "管理员接口")
@Slf4j
public class CheckAdminiController {
  @Autowired
  private AdministratorsService service;
  @GetMapping("{id}")
  @ApiOperation("根据id查询管理员信息")
  public Result findById(@PathVariable("id") Integer id){
      try {
      Administrators administrators = service.getById(id);
      return new Result(true , MessageConstant.QUERY_CHECKITEM_SUCCESS , administrators);
    }catch (Exception e){
      e.printStackTrace();
    }
    return new Result(true , MessageConstant.QUERY_CHECKITEM_FAIL);
  }

  @GetMapping("/findPage")
  @ApiOperation("条件分页查询检查项信息")
  public Result findPage(PageQueryVO vo){
    try {
      Page<Administrators> page = service.findPage(vo);
      if(page!=null){
        return new Result(true , MessageConstant.QUERY_CHECKITEM_SUCCESS , page);
      }
    }catch (Exception e){
      e.printStackTrace();
    }
    return new Result(true , MessageConstant.QUERY_CHECKITEM_FAIL);
  }

  @DeleteMapping("{id}")
  @ApiOperation("删除管理员信息")
  public Result delete(@PathVariable("id") Integer id){
    try {
      boolean flag = service.del(id);
      if(flag){
        return new Result(true , MessageConstant.DELETE_CHECKITEM_SUCCESS);
      }
    }catch (RuntimeException e){
      return new Result(false , e.getMessage());
    }catch (Exception e){
      e.printStackTrace();
    }
    return new Result(false , MessageConstant.DELETE_CHECKITEM_FAIL);
  }
  @PostMapping("addAdmini")
  @ApiOperation("新增管理员信息")
  public Result addAdmini(@RequestBody Administrators administrators){
    try {
      boolean save = service.save(administrators);
      if (save){
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return new Result(false , MessageConstant.ADD_CHECKITEM_FAIL);
  }

  @PutMapping("update")
  @ApiOperation("更新管理员信息")
  public Result update(@RequestBody Administrators administrators){
    try {
      boolean flag = service.updateById(administrators);
      if(flag){
        return new Result(true , MessageConstant.EDIT_CHECKITEM_SUCCESS);
      }
    }catch (Exception e){
      e.printStackTrace();
    }
    return new Result(false , MessageConstant.EDIT_CHECKITEM_FAIL);
  }
//  @PutMapping("{id}")
//  @ApiOperation("更新管理员信息")
//  public Result update(@PathVariable("id") Integer id, @RequestBody Administrators administrators) {
//    try {
//      administrators.setId(id);
//      boolean flag = service.updateById(administrators);
//      if (flag) {
//        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
//      }
//    } catch (Exception e) {
//      e.printStackTrace();
//    }
//    return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
//  }
@PostMapping("upload")
@ApiOperation("套餐图片上传")
public Result upload(MultipartFile imgFile) throws IOException {
  //返回的是图片名字，拼接上七牛云的域名进行显示
//  String pic = service.uploadByQiniuyun(imgFile);

  //返回的是外链地址，可用于直接显示
  String pic = service.uploadByAliyun(imgFile);
  if(StringUtils.hasLength(pic)){
    return new Result(true , MessageConstant.PIC_UPLOAD_SUCCESS , pic);
  }
  return new Result(false , MessageConstant.PIC_UPLOAD_FAIL);
}

}
